//
//  ScanViewController.m
//  IntimateCar_MyProject
//
//  Created by 千锋 on 16/6/7.
//  Copyright © 2016年 mobiletrain. All rights reserved.
//

#import "ScanViewController.h"
#import "ZHScanView.h"
#import <AssetsLibrary/AssetsLibrary.h>
#import "QRScanModel.h"
#import "UILabel+LBExtend.h"
#import "NSString+LBExtend.h"

@interface ScanViewController ()<UIImagePickerControllerDelegate, UINavigationControllerDelegate>

@property (nonatomic, strong) ZHScanView *scanf;

@end

@implementation ScanViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    [self layoutView];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void) layoutView {
    
    UIButton *button = [[UIButton alloc] init];
    [button setImage:[UIImage imageNamed:@"ic_top_back"] forState:UIControlStateNormal];
    [button addTarget:self action:@selector(backAction:) forControlEvents:UIControlEventTouchUpInside];
    
    UIButton *photoScan = [[UIButton alloc] init];
    [photoScan setTitle:@"选择相册" forState:UIControlStateNormal];
    photoScan.titleLabel.font = lbFont(12);
    photoScan.layer.borderColor = kWhiteColor.CGColor;
    photoScan.layer.cornerRadius = 4;
    photoScan.layer.borderWidth = 1;
    [photoScan addTarget:self action:@selector(openPhoto) forControlEvents:UIControlEventTouchUpInside];

    UILabel *title = [[UILabel alloc] init];
    title.text = @"二维码/条纹码";
    title.font = lbFont(14);
    title.textColor = kWhiteColor;

    _scanf = [ZHScanView scanViewWithFrame:self.view.bounds];
    
    __weak typeof(self) weakSelf = self;
    _scanf.showBlock = ^(UIViewController *vc){
    
        [weakSelf presentViewController:vc animated:YES completion:nil];
    };
    
    
    _scanf.promptMessage = @"您可以直接输入或者选择扫描二维码";
    
    [self.view addSubview:_scanf];
    
    [_scanf startScaning];
    [_scanf outPutResult:^(NSString *result) {
        
        if (self.pushBlock)
        {
            self.pushBlock(result, self);
        }
        // 扫描成功后的回调
//        NSLog(@"%@", result);
//        if ([result containsString:@"/scan/qr"]) {
//            
//            [weakSelf requestQRDataWithQRId:result];
//        } else if ([[result substringWithRange:NSMakeRange(0, 7)] isEqualToString:@"http://"] || [[result substringWithRange:NSMakeRange(0, 8)] isEqualToString:@"https://"]) {
//        
//            [[UIApplication sharedApplication] openURL:[NSURL URLWithString:result]];
//        } else {
//        
//            [MBProgressHUD showMessageWithOutIcon:@"二维码不能识别"];
//        }
        
    }];
    
    [_scanf addSubview:title];
    [_scanf addSubview:button];
    [_scanf addSubview:photoScan];
    
    button.sd_layout
    .leftSpaceToView(_scanf, 10)
    .topSpaceToView(_scanf, 20 + 20)
    .widthIs(20)
    .heightIs(20);
    
    title.sd_layout
    .centerYEqualToView(button)
    .centerXEqualToView(_scanf)
    .widthIs([title lb_textWidth])
    .autoHeightRatio(0);
    
    CGSize size = [@"选择相册" sizeWithFont:lbFont(12)];
    photoScan.sd_layout
    .rightSpaceToView(_scanf, 10)
    .centerYEqualToView(button)
    .widthIs(size.width + 5 * 4)
    .heightIs(size.height + 6);
}

// 点击事件
- (void) backAction:(UIButton *) sender {

    [self dismissViewControllerAnimated:YES completion:nil];
}

#pragma mark - 打开相册
- (void) openPhoto {
    
    BOOL ret = [UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary];
    
    //判断是否支持相机
    if (ret) {
        
        UIImagePickerController *picker = [[UIImagePickerController alloc] init];
        [picker setSourceType:UIImagePickerControllerSourceTypePhotoLibrary];
        picker.delegate = self;
        picker.allowsEditing = YES;
        [self presentViewController:picker animated:YES completion:nil];
        
    } else {
        
        NSLog(@"没有相册");
    }
}

#pragma mark - UIImagePickerControllerDelegate
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
    
    [picker dismissViewControllerAnimated:YES completion:^{
        //1.获取编辑的图
        UIImage *image = info[UIImagePickerControllerEditedImage];
        [_scanf recognizeQRCodeFromPhoto:image];
    }];
}

@end
